package com.dbManage.page.home;

import com.dbManage.common.service.TagService;
import com.dbManage.dao.NoteDao;
import com.dbManage.entity.Message;
import com.dbManage.entity.Note;
import com.dbManage.tools.SessionTool;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class HomeController {
    @Resource
    private NoteDao noteDao;
    @Resource
    private TagService tagService;
    @Resource
    private HomeService homeService;

    @ResponseBody
    @RequestMapping("test")
    public String test(){
        return "乱码";
    }
    /**
     * 首页
     * @param model
     * @return
     */
    @RequestMapping("/")
    public String index(Model model){
        model.addAttribute("role", SessionTool.getUser().getRole());
        model.addAttribute("user", SessionTool.getUser());
        return "index";
    }

    /**
     * 搜索页面
     * @param model
     * @return
     */
    @RequestMapping("/searchView")
    public String searchView(Model model){
        model.addAttribute("tagMap",tagService.getTagMap());
        return "search";
    }

    /**
     * 搜索
     * @param note
     * @return
     */
    @RequestMapping("/search")
    @ResponseBody
    public Message search(Note note){
        List<Note> list=homeService.search(note);
        return new Message(Message.STATUS_SUCCESS,list,"success");
    }

    /**
     * 最新已审核的30条
     * @return
     */
    @RequestMapping("/searchNewNote")
    @ResponseBody
    public Message searchNewNote() {
        List<Note> list=homeService.searchNewNote();
        return new Message(Message.STATUS_SUCCESS,list,"success");
    }
    /**
     * 访问最频繁的30条
     * @return
     */
    @RequestMapping("/searchHotNote")
    @ResponseBody
    public Message searchHotNote() {
        List<Note> list=homeService.searchNewNote();
        return new Message(Message.STATUS_SUCCESS,list,"success");
    }

}
